


local meta = FindMetaTable( "Player" )
if (!meta) then return end


/*---------------------------------------------------------
    Is an Admin
---------------------------------------------------------*/
function meta:IsAdmin()

	if ( self:IsSuperAdmin() ) then return true end
	if ( self:IsUserGroup("admin") ) then return true end

	return false
	
end

/*---------------------------------------------------------
    Is a Super Admin
---------------------------------------------------------*/
function meta:IsSuperAdmin()

	return ( self:IsUserGroup("superadmin") )
	
end
	
/*---------------------------------------------------------
    Is Usergroup X
	If you're really customizing your server you can add custom
	usergroups and modify your scripts to accomodate them easily
---------------------------------------------------------*/
function meta:IsUserGroup( name )

	if ( !self:IsValid() ) then return false end
	return ( self:GetNetworkedString( "UserGroup" ) == name )
	
end


/*---------------------------------------------------------
    SetUserGroup
	Sets the user's group (Server only)
---------------------------------------------------------*/
function meta:SetUserGroup( name )

	self:SetNetworkedString( "UserGroup", name )

end


/*---------------------------------------------------------
    This is the meat and spunk of the player auth system
---------------------------------------------------------*/

if ( SERVER ) then

// SteamIds table..
// STEAM_0:1:7099:
//	 	 name	=	garry
//	 	 group	=	superadmin
local SteamIDs = {}

// Load the users file
local UsersKV = util.KeyValuesToTable( file.Read( "../settings/users.txt" ) )

// Extract the data into the SteamIDs table
for key, tab in pairs( UsersKV ) do

	for name, steamid in pairs( tab ) do
	
		SteamIDs[ steamid ] = {}
		SteamIDs[ steamid ].name = name
		SteamIDs[ steamid ].group = key
	
	end

end

local function PlayerSpawn( pl )

	local steamid = pl:SteamID()
	
	if ( SinglePlayer() || pl:IsListenServerHost() ) then
		pl:SetUserGroup( "superadmin" )
		return	
	end
	
	if ( SteamIDs[ steamid ] == nil ) then
		pl:SetUserGroup( "user" )
		return
	end
	
	

	pl:SetUserGroup( SteamIDs[ steamid ].group )
	pl:PrintMessage( HUD_PRINTTALK, "Hey '"..SteamIDs[ steamid ].name.."' - You're in the '"..SteamIDs[ steamid ].group.."' group on this server." )
	
end

hook.Add( "PlayerInitialSpawn", "PlayerAuthSpawn", PlayerSpawn )

end

